﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace DMS.BO.Web.Mvc
{
    /*
     * Usage:
     * [RequireDifferent(ValueToCompare = 0, ErrorMessage = "Vui long chọn tinh thanh")]
     * public int CityID {get;set;}
     */

    public class RequireDifferent : ValidationAttribute, IClientValidatable
    {
        public object ValueToCompare { get; set; }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (ValueToCompare.ToString() == value.ToString())
            {
                return new ValidationResult(this.ErrorMessage);
            }

            return ValidationResult.Success;
        }
        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ModelClientValidationRule mcvrTwo = new ModelClientValidationRule();
            mcvrTwo.ValidationType = "requiredifferent";
            mcvrTwo.ErrorMessage = this.ErrorMessage;
            mcvrTwo.ValidationParameters.Add("param", ValueToCompare);
            return new List<ModelClientValidationRule> { mcvrTwo };
        }
    }
}